package com.ee.chat.cps.admin.service.impl;

import com.ee.chat.common.bean.Result;
import com.ee.chat.common.dao.UserInviteLogMapper;
import com.ee.chat.common.entity.Channel;
import com.ee.chat.cps.admin.service.IChannelOperationService;
import com.ee.chat.cps.admin.service.IHomeService;
import com.ndhutu.ee.util.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author AppiJ
 * @Date 2020/5/15 14:36
 */
@Service
public class HomeServiceImpl implements IHomeService {

    @Resource
    UserInviteLogMapper userInviteLogMapper;
    @Resource
    IChannelOperationService operationService;

    @Override
    public Result channelRegisterStatics(HttpServletRequest request, Integer type) throws Exception {
        Map<String, Object> data = new HashMap<>();

        HttpSession session = request.getSession(true);
        Channel channel = (Channel) session.getAttribute("channelUser");

        String format;     //日期格式
        String startTime;  //开始时间
        if(type == 1){
            format = "%Y-%m-%d";
            startTime = DateUtil.getDayDate();
        }else{
            format = "%Y-%m";
            startTime = DateUtil.getYearDate();
        }

        List<Map<String, Object>> channelRegisterStaticsList =
                userInviteLogMapper.queryChannelRegisterStatics(channel.getUserId(), channel.getId(), format, startTime);
        data.put("channelRegisterStaticsList", channelRegisterStaticsList);

        operationService.addOperationLog(request, "获取渠道邀请注册用户统计数据");

        return Result.returnSuccess(data);
    }
}
